﻿namespace TriangleGames.TrainResEditor
{
    using TriangleGames.TrainRes;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using UnityEditor;
    using UnityEngine;

    public class FsmEditorGUILayout
    {
        public static bool ActionFoldout(bool isOpen)
        {
            return GUILayout.Toggle(isOpen, "", FsmEditorStyles.ActionFoldout, new GUILayoutOption[0]);
        }

        public static string AnimationNamePopup(GameObject gameObject)
        {
            List<string> list = new List<string>();
            if ((gameObject != null) && (gameObject.animation != null))
            {
                foreach (AnimationClip clip in AnimationUtility.GetAnimationClips(gameObject.animation))
                {
                    if (clip != null)
                    {
                        list.Add(clip.name);
                    }
                }
            }
            bool flag = false;
            if (list.Count == 0)
            {
                list.Add("对象没有动画");
                flag = true;
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (flag)
            {
                return "";
            }
            if (num < 0)
            {
                return "";
            }
            return list[num];
        }

        public static AnimationState AnimationStatePopup(GUIContent label, AnimationState selectedAnimationState, GameObject gameObject)
        {
            List<AnimationState> list = new List<AnimationState>();
            string text = "空";
            if (gameObject != null)
            {
                if (gameObject.animation != null)
                {
                    foreach (AnimationState state in gameObject.animation)
                    {
                        list.Add(state);
                    }
                }
                else
                {
                    text = "缺少动画组件";
                }
            }
            else
            {
                text = "选择游戏对象";
            }
            GUIContent[] displayedOptions = new GUIContent[list.Count + 1];
            displayedOptions[0] = new GUIContent(text);
            int selectedIndex = 0;
            for (int i = 0; i < list.Count; i++)
            {
                displayedOptions[i + 1] = new GUIContent(list[i].name);
                if (selectedAnimationState == list[i])
                {
                    selectedIndex = i + 1;
                }
            }
            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, displayedOptions, new GUILayoutOption[0]);
            if (selectedIndex <= 0)
            {
                return null;
            }
            return list[selectedIndex - 1];
        }

        public static Behaviour BehaviorPopup(GUIContent label, Behaviour selectedBehavior, GameObject gameObject)
        {
            Behaviour[] components = new Behaviour[0];
            string text = "空";
            if (gameObject != null)
            {
                components = gameObject.GetComponents<Behaviour>();
            }
            else
            {
                text = "选择游戏对象";
            }
            GUIContent[] displayedOptions = new GUIContent[components.Length + 1];
            displayedOptions[0] = new GUIContent(text);
            int selectedIndex = 0;
            for (int i = 0; i < components.Length; i++)
            {
                displayedOptions[i + 1] = new GUIContent(ObjectNames.GetInspectorTitle(components[i]));
                if (selectedBehavior == components[i])
                {
                    selectedIndex = i + 1;
                }
            }
            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, displayedOptions, new GUILayoutOption[0]);
            if (selectedIndex <= 0)
            {
                return null;
            }
            return components[selectedIndex - 1];
        }

        public static MonoBehaviour BehaviorPopup(GUIContent label, MonoBehaviour selectedBehavior, GameObject gameObject)
        {
            MonoBehaviour[] components = new MonoBehaviour[0];
            string text = "空";
            if (gameObject != null)
            {
                components = gameObject.GetComponents<MonoBehaviour>();
            }
            else
            {
                text = "选择游戏对象";
            }
            GUIContent[] displayedOptions = new GUIContent[components.Length + 1];
            displayedOptions[0] = new GUIContent(text);
            int selectedIndex = 0;
            for (int i = 0; i < components.Length; i++)
            {
                displayedOptions[i + 1] = new GUIContent(ObjectNames.GetInspectorTitle(components[i]));
                if (selectedBehavior == components[i])
                {
                    selectedIndex = i + 1;
                }
            }
            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, displayedOptions, new GUILayoutOption[0]);
            if (selectedIndex <= 0)
            {
                return null;
            }
            return components[selectedIndex - 1];
        }

        public static bool BoldFoldout(bool state, GUIContent content)
        {
            Divider(new GUILayoutOption[0]);
            return EditorGUILayout.Foldout(state, content, FsmEditorStyles.BoldFoldout);
        }

        public static string ComponentNamePopup(GameObject gameObject, System.Type componentType)
        {
            Component[] components = new Component[0];
            if (gameObject != null)
            {
                components = gameObject.GetComponents(componentType);
            }
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            foreach (Component component in components)
            {
                list.Add(FsmEditorUtility.StripUnityEngineNamespace(component.GetType().ToString()));
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static bool DeleteButton()
        {
            FsmEditorStyles.DeleteButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? "删除" : null;
            return MiniButtonPadded(FsmEditorStyles.DeleteButton, new GUILayoutOption[0]);
        }

        public static void DisabledLabel(string text)
        {
            bool enabled = GUI.enabled;
            GUI.enabled = false;
            GUILayout.Label(text, new GUILayoutOption[0]);
            GUI.enabled = enabled;
        }

        public static void Divider(params GUILayoutOption[] layoutOptions)
        {
            GUILayout.Box(GUIContent.none, FsmEditorStyles.Divider, layoutOptions);
        }

        public static bool DoEditorDisabledGUI()
        {
            if (!EditorApplication.isPlaying || !FsmEditorSettings.DisableEditorWhenPlaying)
            {
                return false;
            }
            GUILayout.Label("运行时编辑器无效. 参见设置.", new GUILayoutOption[0]);
            FsmEditorSettings.DisableEditorWhenPlaying = !GUILayout.Toggle(!FsmEditorSettings.DisableEditorWhenPlaying, "运行时编辑器有效", new GUILayoutOption[0]);
            if (GUI.changed)
            {
                FsmEditorSettings.SaveSettings();
            }
            return FsmEditorSettings.DisableEditorWhenPlaying;
        }

        private static bool DoToolWindowsDisabledGUI()
        {
            if (DoEditorDisabledGUI())
            {
                return true;
            }
            if (!EditorApplication.isPlaying || !FsmEditorSettings.DisableToolWindowsWhenPlaying)
            {
                return false;
            }
            GUILayout.Label("运行时工具窗口无效. 参见设置.", new GUILayoutOption[0]);
            FsmEditorSettings.DisableToolWindowsWhenPlaying = !GUILayout.Toggle(!FsmEditorSettings.DisableToolWindowsWhenPlaying, "运行时工具窗口有效", new GUILayoutOption[0]);
            if (GUI.changed)
            {
                FsmEditorSettings.SaveSettings();
            }
            return FsmEditorSettings.DisableToolWindowsWhenPlaying;
        }

        public static FsmEvent EventPopup(GUIContent label, List<FsmEvent> eventList, FsmEvent selectedEvent)
        {
            GUIContent[] eventNamesFromList = FsmEditorUtility.GetEventNamesFromList(eventList);
            string str = (selectedEvent == null) ? null : selectedEvent.Name;
            int selectedIndex = -1;
            int num2 = 0;
            foreach (GUIContent content in eventNamesFromList)
            {
                if (content.text == str)
                {
                    selectedIndex = num2;
                    break;
                }
                num2++;
            }
            int index = EditorGUILayout.Popup(label, selectedIndex, eventNamesFromList, new GUILayoutOption[0]);
            if (index > 0)
            {
                string text = eventNamesFromList[index].text;
                foreach (FsmEvent event2 in eventList)
                {
                    if (event2.Name == text)
                    {
                        return event2;
                    }
                }
            }
            return null;
        }

        public static float FloatSlider(GUIContent label, float value, float minSliderValue, float maxSliderValue)
        {
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if ((label != null) && (label != GUIContent.none))
            {
                GUILayout.Space(6f);
                GUILayout.Label(label, new GUILayoutOption[] { GUILayout.MinWidth(50f), GUILayout.MaxWidth(144f) });
            }
            value = EditorGUILayout.FloatField(value, new GUILayoutOption[] { GUILayout.MaxWidth(75f) });
            float num = value;
            bool changed = GUI.changed;
            GUI.changed = false;
            num = GUILayout.HorizontalSlider(num, minSliderValue, maxSliderValue, new GUILayoutOption[0]);
            if (GUI.changed)
            {
                value = num;
            }
            else
            {
                GUI.changed = changed;
            }
            EditorGUILayout.EndHorizontal();
            return value;
        }

        public static string FsmEventListPopup()
        {
            List<string> list = new List<string>();
            foreach (FsmEvent event2 in FsmEvent.EventList)
            {
                list.Add(event2.Name);
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static string FsmEventPopup(GameObject gameObject, string fsmName)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            foreach (Fsm fsm in FsmEditor.FsmList)
            {
                if ((fsm.GameObject == gameObject) && (fsm.Name == fsmName))
                {
                    foreach (FsmEvent event2 in fsm.Events)
                    {
                        list.Add(event2.Name);
                    }
                }
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static string FsmNamePopup(GameObject gameObject)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            foreach (Fsm fsm in FsmEditor.FsmList)
            {
                if (fsm.GameObject == gameObject)
                {
                    list.Add(fsm.Name);
                }
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static FsmObject FsmObjectPopup(GUIContent label, FsmObject selected, FsmObject[] locals, FsmObject[] globals, System.Type objectType)
        {
            if ((selected != null) && !objectType.IsAssignableFrom(selected.ObjectType))
            {
                selected = null;
            }
            List<GUIContent> list3 = new List<GUIContent>();
            list3.Add(new GUIContent("None"));
            List<GUIContent> list = list3;
            List<FsmObject> list4 = new List<FsmObject>();
            list4.Add(null);
            List<FsmObject> list2 = list4;
            int num = 1;
            int selectedIndex = 0;
            if (locals != null)
            {
                foreach (FsmObject obj2 in locals)
                {
                    if (objectType.IsAssignableFrom(obj2.ObjectType))
                    {
                        list.Add(new GUIContent(obj2.Name));
                        list2.Add(obj2);
                        if ((selected != null) && (obj2 == selected))
                        {
                            selectedIndex = num;
                        }
                        num++;
                    }
                }
            }
            if (globals != null)
            {
                foreach (FsmObject obj3 in globals)
                {
                    if (objectType.IsAssignableFrom(obj3.ObjectType))
                    {
                        list.Add(new GUIContent(obj3.Name + " (global)"));
                        list2.Add(obj3);
                        if ((selected != null) && (obj3 == selected))
                        {
                            selectedIndex = num;
                        }
                        num++;
                    }
                }
            }
            if ((label == GUIContent.none) || (label == null))
            {
                selectedIndex = EditorGUILayout.Popup(selectedIndex, list.ToArray(), new GUILayoutOption[0]);
            }
            else
            {
                selectedIndex = EditorGUILayout.Popup(label, selectedIndex, list.ToArray(), new GUILayoutOption[0]);
            }
            return list2[selectedIndex];
        }

        public static string FsmVariablePopup(GameObject gameObject, string fsmName, UIHint hint)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            foreach (Fsm fsm in FsmEditor.FsmList)
            {
                if ((fsm.GameObject == gameObject) && (fsm.Name == fsmName))
                {
                    foreach (NamedVariable variable in fsm.Variables.GetNames(FsmEditorUtility.GetUIHintVariableType(hint)))
                    {
                        list.Add(variable.Name);
                    }
                }
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static GenericMenu GenerateEventSelectionMenu(Fsm fsm, FsmEvent selectedEvent, GenericMenu.MenuFunction2 selectFunction, GenericMenu.MenuFunction newFunction)
        {
            GenericMenu menu = new GenericMenu();
            bool on = FsmEvent.IsNullOrEmpty(selectedEvent);
            menu.AddItem(new GUIContent("空 (事件)"), on, selectFunction, null);
            foreach (FsmEvent event2 in fsm.Events)
            {
                on = event2 == selectedEvent;
                menu.AddItem(new GUIContent(event2.Name), on, selectFunction, event2);
            }
            menu.AddSeparator(string.Empty);
            menu.AddItem(new GUIContent("新建事件..."), false, newFunction);
            return menu;
        }

        public static GenericMenu GenerateFsmGameObjectSelectionMenu(bool hasAddFsmOption)
        {
            FsmEditor.RebuildFsmList();
            List<GameObject> list = new List<GameObject>();
            foreach (Fsm fsm in FsmEditor.SortedFsmList)
            {
                if (!list.Contains(fsm.GameObject))
                {
                    list.Add(fsm.GameObject);
                }
            }
            GenericMenu menu = new GenericMenu();
            List<string> labels = new List<string>();
            foreach (GameObject obj2 in list)
            {
                if (obj2 != null)
                {
                    string item = FsmEditorUtility.GenerateUniqueLabel(labels, obj2.name);
                    if (FsmEditorSettings.AddPrefabLabel && (EditorUtility.GetPrefabType(obj2) == PrefabType.Prefab))
                    {
                        item = item + " (Prefab)";
                    }
                    labels.Add(item);
                    menu.AddItem(new GUIContent(item), FsmEditor.Selection.ActiveFsmGameObject == obj2, new GenericMenu.MenuFunction2(FsmEditorGUILayout.SelectGameObject), obj2);
                }
            }
            if (hasAddFsmOption)
            {
                menu.AddSeparator(string.Empty);
                if (Selection.activeGameObject != null)
                {
                    string text = "添加状态机到 " + Selection.activeGameObject.name;
                    menu.AddItem(new GUIContent(text), false, new GenericMenu.MenuFunction(FsmEditor.AddFsm));
                    return menu;
                }
                menu.AddDisabledItem(new GUIContent("添加状态机到选定对象..."));
            }
            return menu;
        }

        public static GenericMenu GenerateFsmSelectionMenu(bool hasAddFsmOption)
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("空 (FSM)"), false, new GenericMenu.MenuFunction2(FsmEditor.SelectFsm), null);
            List<string> labels = new List<string>();
            List<Fsm> sortedFsmList = FsmEditor.SortedFsmList;
            sortedFsmList.Sort();
            foreach (Fsm fsm in sortedFsmList)
            {
                if (fsm != null)
                {
                    string item = FsmEditorUtility.GenerateUniqueLabel(labels, FsmEditorUtility.GetFullFsmLabel(fsm));
                    labels.Add(item);
                    menu.AddItem(new GUIContent(item), false, new GenericMenu.MenuFunction2(FsmEditor.SelectFsm), fsm);
                }
            }
            if (hasAddFsmOption)
            {
                menu.AddSeparator(string.Empty);
                if (Selection.activeGameObject != null)
                {
                    string text = "添加状态机到 " + Selection.activeGameObject.name;
                    menu.AddItem(new GUIContent(text), false, new GenericMenu.MenuFunction(FsmEditor.AddFsm));
                    return menu;
                }
                menu.AddDisabledItem(new GUIContent("添加状态机到选定对象..."));
            }
            return menu;
        }

        public static GenericMenu GenerateGameObjectFsmSelectionMenu()
        {
            List<Fsm> list = new List<Fsm>();
            foreach (Fsm fsm in FsmEditor.SortedFsmList)
            {
                if (fsm.GameObject == FsmEditor.Selection.ActiveFsmGameObject)
                {
                    list.Add(fsm);
                }
            }
            GenericMenu menu = new GenericMenu();
            List<string> list3 = new List<string>();
            foreach (Fsm fsm2 in list)
            {
                string fsmLabel = FsmEditorUtility.GetFsmLabel(fsm2);
                list3.Add(fsmLabel);
                menu.AddItem(new GUIContent(fsmLabel), FsmEditor.SelectedFsm == fsm2, new GenericMenu.MenuFunction2(FsmEditor.SelectFsm), fsm2);
            }
            menu.AddSeparator(string.Empty);
            if (FsmEditor.Selection.ActiveFsmGameObject != null)
            {
                string text = "添加状态机到 " + FsmEditor.Selection.ActiveFsmGameObject.name;
                menu.AddItem(new GUIContent(text), false, new GenericMenu.MenuFunction(FsmEditor.AddFsm));
                return menu;
            }
            menu.AddDisabledItem(new GUIContent("添加状态机到选定对象..."));
            return menu;
        }

        public static GenericMenu GenerateStateSelectionMenu(Fsm fsm)
        {
            GenericMenu menu = new GenericMenu();
            foreach (FsmState state in fsm.States)
            {
                menu.AddItem(new GUIContent(state.Name), false, new GenericMenu.MenuFunction2(FsmEditor.SelectStateFromMenu), state.Name);
            }
            return menu;
        }

        public static GenericMenu GenerateStateSelectionMenu(Fsm fsm, string selectedState, GenericMenu.MenuFunction2 selectFunction)
        {
            GenericMenu menu = new GenericMenu();
            bool on = string.IsNullOrEmpty(selectedState);
            menu.AddItem(new GUIContent("空 (State)"), on, selectFunction, null);
            foreach (FsmState state in fsm.States)
            {
                on = state.Name == selectedState;
                menu.AddItem(new GUIContent(state.Name), on, selectFunction, state.Name);
            }
            return menu;
        }

        private static IEnumerable<string> GetMethodNames(MonoBehaviour behaviour, int maxParameters, bool coroutinesOnly)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            if (behaviour != null)
            {
                for (System.Type type = behaviour.GetType(); (type != typeof(MonoBehaviour)) && (type != null); type = type.BaseType)
                {
                    foreach (MethodInfo info in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
                    {
                        if (!coroutinesOnly || (info.ReturnType == typeof(IEnumerator)))
                        {
                            string str2;
                            string name = info.Name;
                            if (((str2 = name) == null) || (((str2 != "Main") && (str2 != "Start")) && (str2 != "Awake")))
                            {
                                ParameterInfo[] parameters = info.GetParameters();
                                if (parameters.Length <= maxParameters)
                                {
                                    if (parameters.Length > 0)
                                    {
                                        if (FsmEditorUtility.IsSupportedParameterType(parameters[0].ParameterType))
                                        {
                                            list.Add(name);
                                        }
                                    }
                                    else
                                    {
                                        list.Add(name);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return list;
        }

        public static GUIContent GetTooltipLabelContent(string labelText, string tooltip)
        {
            if (DragAndDropManager.mode == DragAndDropManager.DragMode.None)
            {
                return new GUIContent(labelText, tooltip);
            }
            return new GUIContent(labelText);
        }

        public static bool HelpButton([Optional, DefaultParameterValue("在线帮助")] string tooltip)
        {
            FsmEditorStyles.HelpButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? tooltip : null;
            return GUILayout.Button(FsmEditorStyles.HelpButton, "Label", new GUILayoutOption[] { GUILayout.MaxWidth(18f) });
        }

        public static bool HelpButtonSmall(string tooltip)
        {
            FsmEditorStyles.HelpButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? tooltip : null;
            return GUILayout.Button(FsmEditorStyles.HelpButton, GUIStyle.none, new GUILayoutOption[] { GUILayout.MaxWidth(18f) });
        }

        public static void LightDivider(params GUILayoutOption[] layoutOptions)
        {
            Color color = GUI.color;
            GUI.color = new Color(1f, 1f, 1f, 0.25f);
            GUILayout.Box(GUIContent.none, FsmEditorStyles.Divider, layoutOptions);
            GUI.color = color;
        }

        public static string MethodNamePopup(GameObject gameobject, MonoBehaviour behaviour, int maxParameters, bool coroutinesOnly)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            if (gameobject != null)
            {
                if (behaviour == null)
                {
                    foreach (MonoBehaviour behaviour2 in gameobject.GetComponents<MonoBehaviour>())
                    {
                        list.AddRange(GetMethodNames(behaviour2, maxParameters, coroutinesOnly));
                    }
                }
                else
                {
                    list.AddRange(GetMethodNames(behaviour, maxParameters, coroutinesOnly));
                }
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static bool MiniButton(GUIContent content, params GUILayoutOption[] layoutOptions)
        {
            return GUILayout.Button(content, FsmEditorStyles.MiniButton, layoutOptions);
        }

        public static bool MiniButtonPadded(GUIContent content, params GUILayoutOption[] layoutOptions)
        {
            return GUILayout.Button(content, FsmEditorStyles.MiniButtonPadded, layoutOptions);
        }

        public static string ParameterTypePopup(string label, string selectedTypeName)
        {
            string[] displayedOptions = new string[] { "空", "布尔", "整型", "浮点", "字符串", "向量", "矩形", "游戏物体", "材质", "贴图", "四元数", "对象" };
            int num = 0;
            int selectedIndex = 0;
            foreach (string str in displayedOptions)
            {
                if (str == selectedTypeName)
                {
                    selectedIndex = num;
                }
                num++;
            }
            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, displayedOptions, new GUILayoutOption[0]);
            if (selectedIndex == -1)
            {
                return "空";
            }
            return displayedOptions[selectedIndex];
        }

        public static void TrainResHeader(EditorWindow window)
        {
            GUI.Box(new Rect(0f, 0f, window.position.width, 60f), "", FsmEditorStyles.TrainResHeader);
            GUILayoutUtility.GetRect(window.position.width, 50f);
        }

        public static INamedVariable Popup(GUIContent label, INamedVariable selected, INamedVariable[] locals, INamedVariable[] globals)
        {
            int num = (locals != null) ? locals.Length : 0;
            if (globals != null)
            {
                num += globals.Length;
            }
            GUIContent[] displayedOptions = new GUIContent[num + 1];
            displayedOptions[0] = new GUIContent("空");
            int selectedIndex = 0;
            int index = 1;
            if (locals != null)
            {
                foreach (INamedVariable variable in locals)
                {
                    displayedOptions[index] = new GUIContent(variable.Name);
                    if (selected == variable)
                    {
                        selectedIndex = index;
                    }
                    index++;
                }
            }
            if (globals != null)
            {
                foreach (INamedVariable variable2 in globals)
                {
                    displayedOptions[index] = new GUIContent(variable2.Name + " (global)");
                    if (selected == variable2)
                    {
                        selectedIndex = index;
                    }
                    index++;
                }
            }
            int num4 = selectedIndex;
            if ((label == GUIContent.none) || (label == null))
            {
                selectedIndex = EditorGUILayout.Popup(selectedIndex, displayedOptions, new GUILayoutOption[0]);
            }
            else
            {
                selectedIndex = EditorGUILayout.Popup(label, selectedIndex, displayedOptions, new GUILayoutOption[0]);
            }
            if (selectedIndex != num4)
            {
                GlobalVariablesWindow.ResetView();
            }
            if (selectedIndex != 0)
            {
                selectedIndex--;
                int num5 = (locals != null) ? locals.Length : 0;
                if (selectedIndex < num5)
                {
                    return locals[selectedIndex];
                }
                selectedIndex -= num5;
                if ((globals != null) && (selectedIndex < globals.Length))
                {
                    return globals[selectedIndex];
                }
            }
            return null;
        }

        public static void ReadonlyTextField(string value, params GUILayoutOption[] layoutOptions)
        {
            bool enabled = GUI.enabled;
            GUI.enabled = false;
            EditorGUILayout.TextField(value, layoutOptions);
            GUI.enabled = enabled;
        }

        public static bool ResetButton()
        {
            FsmEditorStyles.ResetButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? "重置" : null;
            return MiniButton(FsmEditorStyles.ResetButton, new GUILayoutOption[0]);
        }

        public static void ResetGUIColors()
        {
            GUI.backgroundColor = Color.white;
            GUI.contentColor = Color.white;
            GUI.color = Color.white;
        }

        public static void ResetGUIColorsKeepAlpha()
        {
            GUI.backgroundColor = Color.white;
            GUI.contentColor = new Color(1f, 1f, 1f, GUI.contentColor.a);
            GUI.color = new Color(1f, 1f, 1f, GUI.color.a);
        }

        public static bool RightAlignedToggle(GUIContent content, bool value)
        {
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Space(6f);
            GUILayout.Label(content, new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            bool flag = EditorGUILayout.Toggle(value, new GUILayoutOption[] { GUILayout.MaxWidth(15f) });
            EditorGUILayout.EndHorizontal();
            return flag;
        }

        public static string ScriptListPopup()
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            foreach (string str in FsmEditorUtility.ScriptList)
            {
                list.Add(str);
            }
            int num = EditorGUILayout.Popup(-1, list.ToArray(), new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
            if (num <= 0)
            {
                return "";
            }
            return list[num];
        }

        public static string ScriptListPopup(GUIContent label, string selectedComponentName)
        {
            List<string> list2 = new List<string>();
            list2.Add("None");
            List<string> list = list2;
            int selectedIndex = 0;
            int num2 = 1;
            foreach (string str in FsmEditorUtility.ScriptList)
            {
                list.Add(str);
                if (str == selectedComponentName)
                {
                    selectedIndex = num2;
                }
                num2++;
            }
            selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, list.ToArray(), new GUILayoutOption[0]);
            if (selectedIndex <= 0)
            {
                return "";
            }
            return list[selectedIndex];
        }

        private static void SelectGameObject(object userdata)
        {
            GameObject obj2 = (GameObject) userdata;
            bool lockGraphView = FsmEditorSettings.LockGraphView;
            FsmEditorSettings.LockGraphView = false;
            Selection.activeGameObject = obj2;
            FsmEditor.Instance.OnSelectionChange();
            FsmEditorSettings.LockGraphView = lockGraphView;
        }

        public static bool SettingsButton()
        {
            FsmEditorStyles.SettingsButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? "设置" : null;
            return GUILayout.Button(FsmEditorStyles.SettingsButton, GUIStyle.none, new GUILayoutOption[] { GUILayout.MaxWidth(20f) });
        }

        public static bool SettingsButtonPadded()
        {
            FsmEditorStyles.SettingsButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? "设置" : null;
            return GUILayout.Button(FsmEditorStyles.SettingsButton, "Label", new GUILayoutOption[] { GUILayout.MaxWidth(18f) });
        }

        public static int TableRow(GUIContent[] columnText, float[] columnWidths, bool selected, bool hasError, params GUILayoutOption[] layoutOptions)
        {
            int num = -1;
            float fixedHeight = FsmEditorStyles.TableRow.fixedHeight;
            Rect position = GUILayoutUtility.GetRect(0f, 0f, fixedHeight, fixedHeight, layoutOptions);
            if (selected)
            {
                GUI.Box(position, "", FsmEditorStyles.TableRowSelected);
            }
            if ((Event.current.type == EventType.Repaint) && hasError)
            {
                Rect rect3 = new Rect(position);
                rect3.width = 14f;
                rect3.height = 14f;
                Rect rect2 = rect3;
                rect2.x += 2f;
                rect2.y += 2f;
                GUI.DrawTexture(rect2, FsmEditorStyles.Errors);
            }
            position.x += 18f;
            position.width -= 18f;
            int index = 0;
            float x = position.x;
            foreach (GUIContent content in columnText)
            {
                Rect rect5 = new Rect(position);
                rect5.x = x;
                rect5.width = position.width * columnWidths[index];
                Rect rect4 = rect5;
                if (GUI.Button(rect4, content, selected ? FsmEditorStyles.TableRowTextSelected : FsmEditorStyles.TableRowText))
                {
                    num = index;
                }
                x += rect4.width;
                index++;
            }
            return num;
        }

        public static string TextAreaWithHint(string text, string hint, params GUILayoutOption[] layoutOptions)
        {
            bool changed = GUI.changed;
            Color contentColor = GUI.contentColor;
            string str = text;
            if (string.IsNullOrEmpty(str))
            {
                str = hint;
                Color textColor = EditorStyles.label.normal.textColor;
                textColor.a = 0.5f;
                GUI.contentColor = textColor;
            }
            GUI.changed = false;
            str = EditorGUILayout.TextArea(str, FsmEditorStyles.TextAreaWithWordWrap, layoutOptions);
            GUI.contentColor = contentColor;
            if (GUI.changed)
            {
                return str;
            }
            GUI.changed = changed;
            return text;
        }

        public static bool TextFieldButtonWithHint(string text, string hint, params GUILayoutOption[] layoutOptions)
        {
            Color contentColor = GUI.contentColor;
            if (string.IsNullOrEmpty(text))
            {
                text = hint;
                Color textColor = EditorStyles.label.normal.textColor;
                textColor.a = 0.5f;
                GUI.contentColor = textColor;
            }
            bool flag = GUILayout.Button(text, EditorStyles.textField, layoutOptions);
            GUI.contentColor = contentColor;
            return flag;
        }

        public static string TextFieldWithHint(string text, string hint, params GUILayoutOption[] layoutOptions)
        {
            bool changed = GUI.changed;
            Color contentColor = GUI.contentColor;
            string str = text;
            if (string.IsNullOrEmpty(str))
            {
                str = hint;
                Color textColor = EditorStyles.label.normal.textColor;
                textColor.a = 0.5f;
                GUI.contentColor = textColor;
            }
            GUI.changed = false;
            str = EditorGUILayout.TextField(str, layoutOptions);
            GUI.contentColor = contentColor;
            if (GUI.changed)
            {
                return str;
            }
            GUI.changed = changed;
            return text;
        }

        public static bool ToolbarDeleteButton()
        {
            FsmEditorStyles.DeleteButton.tooltip = (DragAndDropManager.mode == DragAndDropManager.DragMode.None) ? "删除" : null;
            return GUILayout.Button(FsmEditorStyles.DeleteButton, EditorStyles.toolbarButton, new GUILayoutOption[] { GUILayout.MaxWidth(24f) });
        }

        public static bool ToolbarSettingsButton()
        {
            return GUILayout.Button(FsmEditorStyles.SettingsButton, EditorStyles.label, new GUILayoutOption[] { GUILayout.MaxWidth(24f) });
        }

        public static void ToolWindowLargeTitle(EditorWindow window, string title)
        {
            GUI.Box(new Rect(0f, 0f, window.position.width, 42f), title, FsmEditorStyles.LargeTitleWithLogo);
            GUILayoutUtility.GetRect(window.position.width, 32f);
        }

        public static bool ToolWindowsCommonGUI(EditorWindow window)
        {
            if (FsmEditor.Instance == null)
            {
                window.Close();
                return false;
            }
            if (DoToolWindowsDisabledGUI())
            {
                return false;
            }
            if (EditorApplication.isCompiling)
            {
                GUI.enabled = false;
            }
            if (!FsmEditorStyles.IsInitialized())
            {
                FsmEditorStyles.Init();
            }
            if ((Event.current.type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.F1))
            {
                FsmEditorSettings.ShowHints = !FsmEditorSettings.ShowHints;
                FsmEditorSettings.SaveSettings();
                window.Repaint();
                return false;
            }
            return true;
        }
    }
}

